import { RouteRecordRaw } from 'vue-router'

// 定义路由配置接口
interface RouteConfig {
  path: string
  name: string
  component: string
  meta: {
    requiresAuth: boolean
    title?: string
    icon?: string
    permissions?: string[]
  }
  children?: RouteConfig[]
}

// 组件映射表
const componentMap: Record<string, () => Promise<any>> = {
  'Dashboard': () => import('@/pages/Dashboard.vue'),
  'Profile': () => import('@/pages/Profile.vue'),
  'Settings': () => import('@/pages/Settings.vue'),
  'Analytics': () => import('@/pages/Analytics.vue')
}

// 将后端路由配置转换为前端路由对象
const transformRoutes = (routes: RouteConfig[]): RouteRecordRaw[] => {
  return routes.map(route => {
    const frontRoute: RouteRecordRaw = {
      path: route.path,
      name: route.name,
      component: componentMap[route.name],
      meta: route.meta
    }

    if (route.children && route.children.length > 0) {
      frontRoute.children = transformRoutes(route.children)
    }

    return frontRoute
  })
}

// 模拟获取用户权限
export const fetchUserPermissions = async (): Promise<string[]> => {
  // 这里模拟异步请求
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(['analytics:view', 'analytics:detail'])
    }, 300)
  })
}

// 模拟获取路由配置
export const fetchRoutes = async (): Promise<RouteRecordRaw[]> => {
  // 这里模拟异步请求
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve([
        {
          path: '/dashboard',
          name: 'Dashboard',
          component: () => import('@/pages/Dashboard.vue'),
          meta: { 
            requiresAuth: true,
            title: '仪表盘',
            permission: 'dashboard:view'
          }
        },
        {
          path: '/analytics',
          name: 'Analytics',
          component: () => import('@/pages/Analytics.vue'),
          meta: { 
            requiresAuth: true,
            title: '数据分析',
            permission: 'analytics:view'
          }
        },
        
        {
          path: '/analytics/detail/:id',
          name: 'AnalyticsDetail',
          component: () => import('@/pages/AnalyticsDetail.vue'),
          meta: { 
            requiresAuth: true,
            title: '数据详情',
            permission: 'analytics:detail'
          }
        },
        {
          path: '/profile',
          name: 'Profile',
          component: () => import('@/pages/Profile.vue'),
          meta: { 
            requiresAuth: true,
            title: '个人资料',
            permission: 'profile:view'
          }
        },
        {
          path: '/settings',
          name: 'Settings',
          component: () => import('@/pages/Settings.vue'),
          meta: { 
            requiresAuth: true,
            title: '设置',
            permission: 'settings:view'
          }
        }
      ])
    }, 300)
  })
} 